package fast.mvc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import indzcraft.loader.Reforge;
import indzcraft.loader.Slot;
import indzcraft.ranking.EP;

public class ReforgePanel extends JPanel {
	
	private static final long serialVersionUID = -3873075140560764517L;
	private Slot slot;
	HashMap<JButton,Reforge> items = new HashMap<JButton,Reforge>();
	
	public ReforgePanel(Slot s){
		this.slot = s;
		this.setLayout(null);
	}
	
	public void showSlot(){
		System.out.println(slot.getName());
	}

	public void showItems(Iterator<EP<Reforge>> it,Reforge curItem) {
		int count = 0;
		double max = 0;
		while (it.hasNext()) {
			Reforge item = it.next().getItem();
			double ep = 0.0;
			if(item != null)
				ep = item.getEP();
			if(max == 0) max = ep;
			boolean equiped = false;
			if (item == curItem)
				equiped = true;
			
			String name = "None";
			//String icon = "Reforge-Portrait";
			String icon = "inv_misc_questionmark";
			if(item != null){
				name = item.getFrom() + " -> " + item.getTo();
			}
			
			JButton btnItem = new JButton();
			if(equiped)
				btnItem.setBorder(new LineBorder(Color.GREEN,2));
			else
				btnItem.setBorder(new LineBorder(Color.BLACK,1));
			btnItem.setBounds(0, count*55, 54, 54);

			ChangeIconTask task = new ChangeIconTask(btnItem,icon);
			task.execute();

			items.put(btnItem,item);
			this.add(btnItem);
			
			JLabel labelName = new JLabel(name);
			labelName.setBounds(55, count*55, 215, 20);
			this.add(labelName);
			
			String info = "";
			JLabel labelInfo = new JLabel(info);
			labelInfo.setBounds(55, 20+count*55, 230, 20);
			this.add(labelInfo);
			
			double diff;
            if (curItem != null)
                diff = ep - curItem.getEP();
            else
                diff = ep;
            JLabel label = new JLabel(String.format("%+.2f", diff));
            label.setBounds(155, 20+count*55,160,20);
            label.setHorizontalAlignment(JLabel.RIGHT);
			this.add(label);
			
			RatingPanel rp = new RatingPanel(ep, max);
			rp.setLocation(55, 40+count*55);
			this.add(rp);
			
			count++;
		}
		this.setPreferredSize(new Dimension(300,1+count*55));
	}
	
	public HashMap<JButton,Reforge> getItems(){
		return items;
	}
	
	public void addReforgeChangeListener(JButton i,ActionListener l){
        i.addActionListener(l);
    }
}
